package com.pishou.code.csv;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pishou.ParamConfig;
import com.pishou.code.bean.Comment;
import com.pishou.code.bean.Product;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ProductCSV {

    private static String fileName = ParamConfig.file_path+"/product.csv";

    public static void setProduct(List<Product> list){
        if (list == null){
            return;
        }
        String  list_str = JSON.toJSONString(list);
        File file = createFile(fileName);
        try {
            FileWriter writer;
            writer = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write("Product");
            bw.newLine();
            if(list!=null){
                bw.write(list_str.toString());
            }
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<Product> getProduct(){
        File file = createFile(fileName);
        String json_str="";
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
            while (br.readLine()!=null) {
                json_str = br.readLine();
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Product> list =  new ArrayList<Product>();
        if (json_str.length() > 0 ){
            JSONArray array = JSONArray.parseArray(json_str);
            Iterator it = array.iterator();
            while (it.hasNext()){
                JSONObject j = (JSONObject)it.next();
                Product product = new Product(j);
                List<Comment> comments = JSONArray.parseArray(j.getString("comments"),Comment.class);
                product.setComments(comments);
                List<String> details = new ArrayList<>();
                JSONArray details_array = j.getJSONArray("details");
                if(details_array!=null){
                    Iterator details_it = details_array.iterator();
                    while (details_it.hasNext()){
                        String  str = (String)details_it.next();
                        details.add(str);
                    }
                    product.setDetails(details);
                }
                list.add(product);
            }
        }
        return list;
    }

    public static File createFile(String fileNameTmp){
        File file = new File(fileNameTmp);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
}
